<?php

require_once "header.php";

echo <<<_END
  
<html>
<head>
<title>Search accounts</title>
</head>
<body>
<center>
<form name="form" action="search.php" method="get">
  <input type="text" name="q" />
  <select name="searchtype" id="searchtype">
        <option value="accountnumber">Account Number</option>
        <option value="firstname">First Name</option>
		<option value="lastname">last Name</option>
		<option value="streetaddress">Street Address</option>
		<option value="city">City</option>
		<option value="state">State</option>
        <option value="zipcode">Zip Code</option>
		<option value="homephone">Home Phone</option>
        <option value="cellphone">Cell Phone</option>
      </select>
  <input type="submit" name="Submit" value="Search" />
</form>
</center>
</body>
</html>
_END;
  // Get the search variable from URL
$test = $_GET['searchtype'];

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10;

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<center><p>Please enter a search...</p></center>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database 
mysql_connect("localhost","admin","admin"); //(host, username, password)

//specify database 
mysql_select_db("test") or die("Unable to select database"); //select which database we're using

//$query="SELECT * FROM accountinfo";
//$rt = mysql_query($query);
//echo mysql_error();
//
//while($nt=mysql_fetch_array($rt)){
//	echo "$nt[firstname]";
//	}

// Build SQL Query
//if ($test == 'accountnumber')
  $query = "SELECT * FROM accountinfo WHERE $test LIKE \"%$trimmed%\"  
  ORDER BY accountnumber";
  //else
  //$query = "SELECT * FROM $test WHERE $test LIKE \"%$trimmed%\"  
  //ORDER BY accountnumber";
  
//$query = "SELECT * FROM accountinfo WHERE accountnumber LIKE \"%$trimmed%\"  
//  ORDER BY accountnumber"; // EDIT HERE and specify your table and field names for the SQL query
 $numresults=mysql_query("SELECT * FROM accountinfo WHERE accountnumber LIKE \"%$trimmed%\"  
  ORDER BY accountnumber");
  
 //$numrows=mysql_num_rows($numresults);
 //$numrows=mysql_num_rows("SELECT * FROM accountinfo WHERE accountnumber LIKE \"%$trimmed%\" ORDER BY accountnumber"); 
  

// If we have no results, offer a google search as an alternative

if ($numresults == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

// google
// echo "<p><a href=\"http://www.google.com/search?q=" 
 // . $trimmed . "\" target=\"_blank\" title=\"Look up 
//  " . $trimmed . " on Google\">Click here</a> to try the 
//  search on google</p>";
  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

if(!$numresults){
		$message .= 'query:' . $query;
		die($message);
		}
// get results
  //$query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<center><p>You searched for: &quot;" . $var . "&quot;</p></center>";

// begin to show results set
echo "<center>Results</center>";
$count = 1 + $s ;

echo "$searchtype";

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["firstname"];
  $lastname = $row["lastname"];
  $accountnumber = $row["accountnumber"];
  $middleinitial = $row["middleinitial"];
  $streetaddress = $row["streetaddress"];
  $city = $row["city"];
  $state = $row["state"];
  $zipcode = $row["zipcode"];
  $homephone = $row["homephone"];
  $cellphone = $row["cellphone"];
  
  

  echo "<br /><br /><br />$count.) Account #: &nbsp $accountnumber &nbsp Name: &nbsp $title &nbsp $lastname &nbsp Address: $streetaddress , $city , $state , $zipcode &nbsp Home Phone: $homephone &nbsp Cell Phone: $cellphone <br />" ;
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
  Prev 10</a>&nbsp&nbsp;";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";

  
?>


